package com.heng.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heng.dao.entity.ShippingAddress;
import com.heng.dao.mapper.ShippingAddressMapper;
import com.heng.service.ShippingAddressService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收货地址服务实现
 */
@Service
public class ShippingAddressServiceImpl extends ServiceImpl<ShippingAddressMapper, ShippingAddress>
        implements ShippingAddressService {

    @Resource
    private ShippingAddressMapper shippingAddressMapper;

    /**
     * 根据用户ID查询所有地址（MP条件构造器）
     */
    @Override
    public List<ShippingAddress> listByUserId(Long userId) {
        LambdaQueryWrapper<ShippingAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShippingAddress::getUserId, userId); // 等值查询：user_id = userId
        return shippingAddressMapper.selectList(queryWrapper);
    }

    /**
     * 查询用户默认地址（is_default=1）
     */
    @Override
    public ShippingAddress getDefaultByUserId(Long userId) {
        LambdaQueryWrapper<ShippingAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShippingAddress::getUserId, userId)
                    .eq(ShippingAddress::getIsDefault, 1); // 同时满足：用户ID+默认地址
        return shippingAddressMapper.selectOne(queryWrapper);
    }

    // 父类ServiceImpl已自带以下基础方法，无需重复实现：
    // - save(ShippingAddress entity)：保存地址
    // - getById(Long id)：根据ID查询地址
    // - updateById(ShippingAddress entity)：更新地址
    // - removeById(Long id)：删除地址
}